package com.example.evensystem.view

import android.app.Application
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.evensystem.model.database.SystemDb
import com.example.evensystem.viewmodel.MainViewModel
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.dsl.module

class MyApplication: Application() {
    private val sqlModule = module{
        //SystemDb单例
        single { Room.databaseBuilder(androidContext(),SystemDb::class.java,"system_db")
            .allowMainThreadQueries()
            .fallbackToDestructiveMigration()
            .build()}

        //各种Dao的单例对象
        single { get<SystemDb>().getAccountDao() }
        single { get<SystemDb>().getDailyWorkDao() }
       // single { get<SystemDb>().getHolidayPlanDao() }
        single { get<SystemDb>().getMonthlyWorkDao() }
        single { get<SystemDb>().getMonthPlanDao() }
        single { get<SystemDb>().getWorkerDao() }
        viewModel { MainViewModel(get()) }
    }



    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidLogger(Level.INFO)
            androidContext(this@MyApplication)
            modules(listOf(sqlModule))
        }
    }
}